﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ivony.Html;
using Ivony.Html.Parser;
using System.Text.RegularExpressions;

namespace ZX.EHR.ResumeParseLib
{
    /// <summary>
    /// 前程无忧
    /// </summary>
    public class Job51Parsing : IHtmlParsable
    {
        public ResumeParsedInfo Parsing(string htmlContent)
        {

            if (string.IsNullOrEmpty(htmlContent))
                throw new ArgumentNullException("htmlContent");
            
            ResumeParsedInfo resumeInfo = null;
            var doc = new JumonyParser().Parse(htmlContent);
            
            if (doc != null)
            {
                resumeInfo = new ResumeParsedInfo();
                resumeInfo.Name = doc.Find("b").ElementAt(0).InnerText(); //姓名
                try
                {
                    string tempStr = doc.Find("b").ElementAt(1).InnerText();
                    string[] token = tempStr.Split("|".ToCharArray());
                    resumeInfo.WorkYears = WordToNumber(token[0].Split("年".ToCharArray())[0]);//工作年限
                    resumeInfo.Gender = token[1].Trim();//性别
                    string temp = tempStr.Split("|".ToCharArray())[2];
                    int index1 = temp.IndexOf("（");
                    int index2 = temp.IndexOf("）");
                    string[] tempTokenY = temp.Substring(index1 + 1, index2 - index1 - 1).Split("年".ToCharArray());
                    string birthDayY = tempTokenY[0].Trim();
                    string[] tempTokenM = tempTokenY[1].Trim().Split("月".ToCharArray());
                    string birthDayM = tempTokenM[0].Trim();
                    string birthDatD = tempTokenM[1].Trim().Split("日".ToCharArray())[0];
                    resumeInfo.BirthDay = birthDayY + "-" + birthDayM + "-" + birthDatD;//出生日期
                }
                catch { }

                try
                {
                    IHtmlNode emailNode = doc.Find("a").ElementAt(1);
                    resumeInfo.Email = emailNode.InnerText();//email
                    IHtmlElement telNode = emailNode.Parent().Parent().PreviousNode() as IHtmlElement;
                    resumeInfo.PhoneNO = telNode.Find("td").ElementAt(1).InnerText().Substring(0,11);//电话
                    
                    IList<IHtmlNode> eList2 = doc.Find("b").ElementAt(2).Parent().Parent().Parent().SiblingNodesAfterSelf().ToList<IHtmlNode>();
                    IHtmlElement ihtmlElement2 = eList2[1] as IHtmlElement;
                    string indusry = ihtmlElement2.Find("td").ElementAt(1).InnerText();//所在行业
                    ihtmlElement2 = eList2[2] as IHtmlElement;
                    resumeInfo.NowPosition = ihtmlElement2.Find("td").ElementAt(1).InnerText();//目前职位

                    IList<IHtmlNode> eList4 = doc.Find("b").ElementAt(4).Parent().Parent().Parent().SiblingNodesAfterSelf().ToList<IHtmlNode>();
                    ihtmlElement2 = eList4[0] as IHtmlElement;
                    resumeInfo.Degree = ihtmlElement2.Find("td").ElementAt(1).InnerText();//学历
                    ihtmlElement2 = eList4[1] as IHtmlElement;
                    string zhuanye = ihtmlElement2.Find("td").ElementAt(1).InnerText();//专业
                }
                catch
                { }
                resumeInfo.FullHtmlContent = htmlContent;
            }
            return resumeInfo;
        }

        static string WordToNumber(string word)
        {
            string[] a ={"一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
                       "二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十"};
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == word.Trim()) return (i+1).ToString();
            }
            return "1";
        }
    }
}
